package View;

import Repositories.UsuarioRepository;
import java.awt.AWTKeyStroke;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.netbeans.lib.awtextra.AbsoluteLayout;

public class Login extends javax.swing.JFrame {

    public Login() {
        this.setUndecorated(true);
        initComponents();

        AbsoluteLayout laiout = null;
        jPanel1.setLayout(laiout);

        //bg login
        JLabel labelLogin = new JLabel(new ImageIcon("./img/login2.png"));
        jPanel1.add(labelLogin);
        labelLogin.setBounds(0, 0, 370, 190);

        //bg butao entrar
        JLabel label2 = new JLabel(new ImageIcon("./img/bt_entrar.png"));
        jbt_Entrar.add(label2);
        jbt_Entrar.setContentAreaFilled(false);
        jbt_Entrar.setBorderPainted(false);
        jbt_Entrar.setToolTipText("Entrar [Enter]");

        //bg bt peq x
        JLabel label8 = new JLabel(new ImageIcon("./img/bt_peq_x.png"));
        jbt_x.add(label8);
        jbt_x.setContentAreaFilled(false);
        jbt_x.setBorderPainted(false);
        jbt_x.setToolTipText("Fechar [Alt+F4]");

        this.setLocationRelativeTo(null);

        txtLogin.grabFocus();
        Enter();
    }

    private void Enter() {
        HashSet conj = new HashSet(txtLogin.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
        conj.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_ENTER, 0));
        txtLogin.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, conj);

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtSenha = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        txtLogin = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jbt_Entrar = new javax.swing.JButton();
        jbt_x = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 0, 204));
        jPanel1.setMinimumSize(new java.awt.Dimension(370, 190));
        jPanel1.setPreferredSize(new java.awt.Dimension(370, 190));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtSenha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSenhaActionPerformed(evt);
            }
        });
        jPanel1.add(txtSenha, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 110, 150, -1));

        jLabel2.setText("Senha");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 90, -1, -1));

        txtLogin.setNextFocusableComponent(txtSenha);
        jPanel1.add(txtLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 60, 155, -1));

        jLabel1.setText("Login");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 40, -1, -1));

        jbt_Entrar.setBorder(null);
        jbt_Entrar.setContentAreaFilled(false);
        jbt_Entrar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jbt_EntrarMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jbt_EntrarMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jbt_EntrarMousePressed(evt);
            }
        });
        jbt_Entrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbt_EntrarActionPerformed(evt);
            }
        });
        jPanel1.add(jbt_Entrar, new org.netbeans.lib.awtextra.AbsoluteConstraints(236, 57, 83, 74));

        jbt_x.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jbt_xMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jbt_xMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jbt_xMousePressed(evt);
            }
        });
        jbt_x.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbt_xActionPerformed(evt);
            }
        });
        jPanel1.add(jbt_x, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 0, 40, 40));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void Entrar() {
        if(ChecarCampos())
        {
        UsuarioRepository usuarioRepository = new UsuarioRepository();
        if (usuarioRepository.Autenticas(txtLogin.getText(), txtSenha.getText())) {
            this.dispose();
            Inicio inicio = new Inicio();
            inicio.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "Usuário ou senha inexistente!");
        }
        }
    }

    private boolean ChecarCampos()
    {
        if(txtLogin.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null,"É obrigatório preencher o campo Login!");
            return false;
        }
        else if(txtSenha.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null,"É obrigatório preencher o campo Senha!");
            return false;
        }
        return true;
    }
    
private void jbt_EntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbt_EntrarActionPerformed
    Entrar();
}//GEN-LAST:event_jbt_EntrarActionPerformed

private void jbt_xActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbt_xActionPerformed
    System.exit(0);
}//GEN-LAST:event_jbt_xActionPerformed

private void jbt_EntrarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbt_EntrarMouseEntered
    jbt_Entrar.removeAll();
    Icon img1c = new ImageIcon("./img/bt_entrar_f.png");
    jbt_Entrar.setIcon(img1c);
}//GEN-LAST:event_jbt_EntrarMouseEntered

private void jbt_EntrarMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbt_EntrarMouseExited
    jbt_Entrar.removeAll();
    Icon img1c = new ImageIcon("./img/bt_entrar.png");
    jbt_Entrar.setIcon(img1c);
}//GEN-LAST:event_jbt_EntrarMouseExited

private void jbt_EntrarMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbt_EntrarMousePressed
    Icon img1c = new ImageIcon("./img/bt_entrar_c.png");
    jbt_Entrar.setIcon(img1c);
}//GEN-LAST:event_jbt_EntrarMousePressed

private void jbt_xMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbt_xMouseEntered
    jbt_x.removeAll();
    Icon img1c = new ImageIcon("./img/bt_peq_x_f.png");
    jbt_x.setIcon(img1c);

}//GEN-LAST:event_jbt_xMouseEntered

private void jbt_xMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbt_xMouseExited
    // jbt_x.removeAll();
    Icon img1c = new ImageIcon("./img/bt_peq_x.png");
    jbt_x.setIcon(img1c);
}//GEN-LAST:event_jbt_xMouseExited

private void jbt_xMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbt_xMousePressed
    Icon img1c = new ImageIcon("./img/bt_peq_x_c.png");
    jbt_x.setIcon(img1c);
}//GEN-LAST:event_jbt_xMousePressed

private void txtSenhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSenhaActionPerformed
    Entrar();
}//GEN-LAST:event_txtSenhaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Login().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbt_Entrar;
    private javax.swing.JToggleButton jbt_x;
    private javax.swing.JTextField txtLogin;
    private javax.swing.JPasswordField txtSenha;
    // End of variables declaration//GEN-END:variables
}
